package task.nms;

import java.sql.Connection;
import java.sql.SQLException;
import java.util.Map;

import task.DbTask;
import util.JdbcUtil;

public abstract class NmsTask extends DbTask {
	public static final String[] TABLES = {
		"axxrpc_user","core_discover_settings","core_general_language","core_general_literal",
		"core_general_sequence","core_general_translation","core_map_data",
		"core_map_item_geometry","core_map_item_position","core_mrs_eventlogger",
		"core_mrs_permissions_resources","core_mrs_resource","core_mrs_subscription",
		"core_preference","core_security_action","core_security_audit","core_security_cont_inf_types",
		"core_security_contact_info","core_security_group","core_security_group_actions",
		"core_security_group_resources","core_security_permission_group","core_security_resource",
		"core_security_user","core_security_user_groups","db_current_version","db_upgrade_history",
		"med_discovered_elements","med_element_config_backup","med_performance_data","med_pmp",
		"med_pmp_thresholds","med_q1_to_sdname_map","med_tca_parameter_profiles","med_tca_parameters",
		"med_tca_profile_assignment","meta_model","meta_repository","nif_snmp_forwarding_params",
		"nif_snmp_managers","nif_snmp_model_properties","nif_snmp_security_models",
		"perf_tca_profile_mapping","perf_tca_profile_properties","perf_tca_profiles",
		"q1coll_adapter_elements","q1coll_element_gateway_map","q1coll_element_state",
		"q1coll_gateways","q1coll_message_store","q1coll_node_info","q1coll_polling_message_store",
		"rc_filter","state_administrative_domains","state_alarms","state_asa","state_asap",
		"state_common_resource","state_common_resource_registry","state_cross_a_end",
		"state_cross_connect","state_cross_z_end","state_ctp","state_ems","state_equipment",
		"state_equipment_holders","state_event_comments","state_managed_elements",
		"state_me_security_template","state_performance_data","state_pmps","state_ptp",
		"state_ptps_on_equipment","state_resources_in_domains","state_station_entities",
		"state_stations","state_sw_binary_information","state_sw_download_jobs",
		"state_sw_download_registry","state_sw_info","state_sw_job_me_relations",
		"state_target_domains","state_topological_links","state_transmission_parameters","timers"
	};
	protected String nmsVersion;
	@Override
	public void setConnection(Connection conn) {
		super.setConnection(conn);
//		if (conn != null) {
//			setNmsVersion();
//		}
	}
    public String adjustSql(String sql) {
    	char version = nmsVersion.charAt(3);
    	String result = null;
    	switch (version) {
    	case 'G':
    	case 'H':
    	case 'J':
    		result = adjust(sql, "managed_state","include_state");
    		break;
    	case 'K':
    		result = adjust(sql, "include_state","managed_state");
    		break;
    	}
    	return result;
    }
	private String adjust(String sql, String from, String to) {
		sql = sql.replaceAll(from.toLowerCase(), to.toLowerCase());
		sql = sql.replaceAll(from.toUpperCase(), to.toUpperCase());
		return sql;
	}
	public String getNmsVersion(String schema) {
		String sql = String.format("select version from %s.DB_CURRENT_VERSION", schema);
		try {
			String nmsVersion = JdbcUtil.getSingleString(conn(), sql);
			return nmsVersion;
		} catch (SQLException e) {
			throw new RuntimeException(e);
		}
	}
	public void setNmsVersion(String nmsVersion) {
		this.nmsVersion = nmsVersion;
	}
}
